package com.darren.euc.service.impl;

import java.math.BigInteger;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.darren.euc.dao.MessageVisitDao;
import com.darren.euc.entity.MessageVisit;
import com.darren.euc.entity.PageBean;
import com.darren.euc.service.MessageVisitService;
import com.darren.euc.util.StringUtil;

import net.sf.json.JSONObject;


@Service
public class MessageVisitServiceImpl implements MessageVisitService {
	
	private static final Logger log = Logger.getLogger(MessageVisitServiceImpl.class);
	
	@Autowired
    private MessageVisitDao messageVisitDao;

    @Override
    public MessageVisit save(MessageVisit messageVisit) {
        return messageVisitDao.save(messageVisit);
    }

    @Override
    public MessageVisit update(MessageVisit messageVisit) {
        return messageVisitDao.update(messageVisit);
    }

    @Override
    public MessageVisit findById(Integer id){
    	return messageVisitDao.findById(id);
    }
    
    public BigInteger getUnreadCount(Integer userId){
    	return messageVisitDao.getUnreadCount(userId);
    }
    
	@SuppressWarnings("rawtypes")
	public PageBean queryForPageUseHQL(JSONObject requestJson,Integer pageSize, Integer currentPage){		
		String HQL = null;
		String likes = null;
		String equals = null;
			List<Object> parameters = new ArrayList<Object>();
		JSONObject likeJson = requestJson.getJSONObject("like");
		Iterator likeIterator = likeJson.keys();
		while(likeIterator.hasNext()){
			String key = (String) likeIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = likeJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				likes = (likes == null?"": likes + " OR ") +  key + " like ? ";
				parameters.add("%"+value+"%");
			}
		}
		HQL = likes == null?null:(" (" + likes + ") ");		
		JSONObject equalsJson = requestJson.getJSONObject("equals");
        Iterator equalsIterator = equalsJson.keys();        
		while(equalsIterator.hasNext()){
			String key = (String) equalsIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = equalsJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){				
				if ("startTime".equals(key)) {
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					try {
						Date startDate = (Date) format.parse(value + " 00:00:00");
						parameters.add(startDate);
					} catch (ParseException e) {
						log.error(e.getMessage(), e);
					}
					equals = (equals == null?"": equals + " and ") + " submitTime >= ? ";
				} else if ("endTime".equals(key)) {
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					try {
						Date endDate = (Date) format.parse(value + " 23:59:59");
						parameters.add(endDate);
					} catch (ParseException e) {
						log.error(e.getMessage(), e);
					}
					equals = (equals == null?"": equals + " and ") + " submitTime <= ? ";
				} else {
					equals = (equals == null?"": equals + " and ") +  key + " = ? ";
					parameters.add(value);
				}
			}
		}		
		if(HQL != null){
			if(equals != null){
				HQL = HQL + " and " + equals;
			}
		}else{
			HQL = equals;
		}
		
		HQL = String.format(" from MessageVisit where userId = %s ", requestJson.getInt("userId")) + (HQL == null?"":" and " + HQL) + " order by createTime desc ";
		return messageVisitDao.queryForPageByHQL(HQL, pageSize, currentPage,parameters.toArray());
	}
}